import { handlename } from './handleRouteName';
import routerObj from '../config';
// 引入 views 文件夹下所有 vue 文件
const modules = import.meta.glob('@/views/**/index.tsx', {});
console.log(modules);

// 数组去重方法
function distict(data: any[]) {
  return [...new Set(data)];
}

// 转为树形结构
export function filterRoute(data: any[], i = 1, name: string = '') {
  const routes = distict(data.map((item) => item[i])).map((it) => {
    const { path, routeName } = handlename(it, name);
    const pathName = '/src/views' + routeName.replace('menus', '') + '/index.tsx';

    let level = i + 1;
    let row: any = {
      path,
      name: routeName,
      meta: { ...routerObj[routeName] },
      component: modules[pathName],
    };
    const children = data.filter((rr) => rr[i] == it && rr[level] != null);

    if (children.length) {
      delete row.component;
      if (!row.meta) {
        delete row.meta;
      }
      row.redirect = path + '/' + children[0][children[0].length - 1];
      row.children = filterRoute(children, level, row.name);
    }
    return row;
  });

  return routes;
}
